package com.zlc.workflow.flow.node;

import com.zlc.common.util.AssertUtils;
import org.springframework.util.ObjectUtils;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import static com.zlc.workflow.constant.NodeType.START;

/**
 *  开始节点
 *
 * @author zhanglei
 */
public class StartNode extends BaseNode {


    public StartNode(String nodeId,String nodeNum, String nodeName) {
        super(nodeId,nodeNum, nodeName);
        this.nodeType = START;
    }

    public void build(){
        AssertUtils.notEmpty(element,"DOC is null ! build process fail ");
        now = ObjectUtils.isEmpty(now) ? getStartNode() : now;
        AssertUtils.notEmpty(now,"the process context is error ! please update process!");

        //构建路线
        buildPaths(now);
        //构建节点指针
        createNodePointer();
    }



    private Element getStartNode(){
        NodeList nodeList = element.getChildNodes();

        for(int i=0 ; i<nodeList.getLength() ; i++){
            Node node = nodeList.item(i);
            if(node instanceof Element && node.getNodeName().equals(START.getValue())){
                now = (Element) node;
                break;
            }
        }
        return now;
    }

}
